/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lk.asha.desktop.beans;

import java.util.Iterator;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import lk.asha.desktop.database.Customer;
import lk.asha.desktop.database.Item;
import lk.asha.desktop.database.Sms;
import lk.asha.desktop.database.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author user
 */
public class DatabaseClass {
      Session session = null;
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
   
    
      
      
  public boolean checkPassword(String table, String username, String password){

  try{
 session =sessionFactory.openSession();




    //Create Select Clause HQL
     String SQL_QUERY ="FROM "+table+" where username='"+username+"' and password='"+password+"' and( privilege=3 or privilege=4)";
     System.out.println(SQL_QUERY);
 Query query = session.createQuery(SQL_QUERY);
 for(Iterator it=query.iterate();it.hasNext();){
       User row = (User) it.next();
       System.out.println("ID: " + row.getUserId());
       System.out.println("Name: " + row.getUsername());

     return true;
     }
JOptionPane.showMessageDialog (null, "Enter correct username and password.", "Title", JOptionPane.WARNING_MESSAGE);
 //session.close();

  }catch(Exception e){
    JOptionPane.showMessageDialog (null, "Database can't be found", "Title", JOptionPane.ERROR_MESSAGE);
    System.out.println(e.getMessage());
  }finally{
       
    }

  


    return false;
    }
public boolean insertData(Object object){
     try{
      session =sessionFactory.openSession();
                 //Create new instance of Contact and set values in it by reading them from form object
                 System.out.println("Inserting Record");
                 Transaction tx = session.beginTransaction();
                 session.save(object);
                 tx.commit();

                 System.out.println("Done inserting values ");
         }catch(Exception e){
             System.out.println(e.getMessage());

         }finally{
             // Actual contact insertion will happen at this step
            // session.evict(contact);
          //   session.clear();
             session.flush();
             session.close();


return false;
}
}
public  Query getComboBoxValue(String sql){
//  String data="";
    System.out.println(sql);
  try{
 session =sessionFactory.openSession();
    //Create Select Clause HQL
    
 Query query = session.createQuery(sql);
 
// session.close();
 return query;
  }catch(Exception e){
    //JOptionPane.showMessageDialog (null, "Database can't be found Error code 1", "Title", JOptionPane.ERROR_MESSAGE);
    System.out.println(e.getMessage());
  }finally{

    }

return null;
}


public  String[][] getSMS(String sql){
//  String data="";
    System.out.println(sql);
  try{
 session =sessionFactory.openSession();
    //Create Select Clause HQL
    
 Query query = session.createQuery(sql);
  for(Iterator it=query.iterate();it.hasNext();){
          Customer customer=(Customer)it.next();
                    customer.getRequesteditems().toArray();
          System.out.println(customer.getRequesteditems().size());
     //   data=data+","+employ.getName();
     //   
             }
// session.close();
//p return query;
//  }catch(Exception e){
    //JOptionPane.showMessageDialog (null, "Database can't be found Error code 1", "Title", JOptionPane.ERROR_MESSAGE);
 //   System.out.println(e.getMessage());
  }finally{

    }

return null;
}

public  List executeHQLQuery(String hql) {
    try {
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = (List) q.list();
      //displayResult(resultList);

        session.getTransaction().commit();

        return resultList;
    } catch (HibernateException he) {
        he.printStackTrace();
    }
    return null;
}

public boolean userValidation(String table,String username,String password){
String sql="from "+table+" where name='"+username+"'and password='"+password+"'";

List resultList=executeHQLQuery(sql);
for(Object o : resultList) {
       return true;
    }

return false;
}

public static  boolean insertData(String table, Object obj){
    String hql=table;
 try {
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(obj);
        System.out.println("Successfully data insert in database");

        session.getTransaction().commit();


    } catch (HibernateException he) {
        he.printStackTrace();
    }

return false;
}

public static String addSuccessfully(){
return "Ad";
}

 public String  getRow(String table,String colome,String value,int col ){
  try{
 session =sessionFactory.openSession();
    //Create Select Clause HQL
     String SQL_QUERY ="FROM "+table+" where "+colome+"='"+value+"'";
     System.out.println(SQL_QUERY);
 Query query = session.createQuery(SQL_QUERY);
 for(Iterator it=query.iterate();it.hasNext();){
       if (table.equals("User")) {
         User row = (User) it.next();
             System.out.println(row.getPrivilege());
         return row.getPrivilege();
     } else if(table.equals("User")){
     }

//

     }
 session.close();

  }catch(Exception e){
    //JOptionPane.showMessageDialog (null, "Database can't be found Error code 1", "Title", JOptionPane.ERROR_MESSAGE);
    System.out.println(e.getMessage());
  }finally{

    }


 return "";
 }

public  Object getObject(String sql){
   // String data="";
  try{
 session =sessionFactory.openSession();
    //Create Select Clause HQL

 Query query = session.createQuery(sql);
  for(Iterator it=query.iterate();it.hasNext();){
                return  it.next();

             }
// session.close();
 return query;
  }catch(Exception e){
    //JOptionPane.showMessageDialog (null, "Database can't be found Error code 1", "Title", JOptionPane.ERROR_MESSAGE);
    System.out.println(e.getMessage());
  }finally{

    }

return null;
}


}
